credit card validation in c#

45

credit card validation in c# -

public bool IsValidCard()
{
    if (txtCardNumber.Text.StartsWith("1298") ||
        txtCardNumber.Text.StartsWith("1267") ||
        txtCardNumber.Text.StartsWith("4512") ||
        txtCardNumber.Text.StartsWith("4567") ||
        txtCardNumber.Text.StartsWith("8901") ||
        txtCardNumber.Text.StartsWith("8933"))
    {
        if (Regex.Replace(txtCardNumber.Text, @"\s+", "").Length == 16)
        {
            return true;
        }
    }
    return false;
}//end IsValidCard()

public bool IsValidSecurityCode()
{
    bool isValid = Regex.Match(txtSecurityCode.Text, @"^\d{3}$").Success;
    return isValid;
}//end IsValidSecurityCode()

public bool IsValidExpiration()
{
    string[] date = Regex.Split(txtExpiration.Text, "/");
    string[] currentDate = Regex.Split(DateTime.Now.ToString("MM/yyyy"), "/");
    int compareYears = string.Compare(date[1], currentDate[1]);
    int compareMonths = string.Compare(date[0], currentDate[0]);

    //if expiration date is in MM/YYYY format
    if (Regex.Match(txtExpiration.Text, @"^\d{2}/\d{4}$").Success)
    {
        //if month is "01-12" and year starts with "20"
        if (Regex.Match(date[0], @"^[0][1-9]|[1][0-2]$").Success)
        {
            //if expiration date is after current date
            if ((compareYears == 1) || (compareYears == 0 && (compareMonths == 1)))
            {
                return true;
            }
        }
    }
    return false;
}//end IsValidExpiration

Comments

Submit
0 Comments